﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GPSTrackingWebsiteManager.Manager.UserControls
{
    public partial class NotificationsSearch : System.Web.UI.UserControl
    {
        //Events
        public delegate void AddNotificationsEvent(Telerik.Web.UI.GridDataItem row);
        public event AddNotificationsEvent OnAddNotificationsEvent;

        public delegate void AcceptEvent(Telerik.Web.UI.GridDataItem row);
        public event AcceptEvent OnAcceptEvent;

        //Propeties
        [Browsable(true), Category("Custom"), DefaultValue(true)]
        public bool IsAddNotificationsVisible { get; set; }

        public int GetNotificationsID()
        {
            if (string.IsNullOrEmpty(txtNotificationsID.Text))
                return -1;
            else
                return Convert.ToInt32(txtNotificationsID.Text);
        }

        public void ClearControls()
        {
            txtNotificationsID.Text = string.Empty; ;
            txtNotificationsName.Text = string.Empty;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            btnAddNotifications.Visible = IsAddNotificationsVisible;
        }

        protected void btnNotificationsSearch_Click(object sender, EventArgs e)
        {
            GPSTrackingDBV2.Data.NotificationsQuery NotificationsQuery = new GPSTrackingDBV2.Data.NotificationsQuery();
            if (!string.IsNullOrEmpty(txtSearchNotificationsID.Text)) NotificationsQuery.AppendContains(GPSTrackingDBV2.Entities.NotificationsColumn.NotificationId, txtSearchNotificationsID.Text);
            if (!string.IsNullOrEmpty(txtSearchNotificationsName.Text)) NotificationsQuery.AppendContains(GPSTrackingDBV2.Entities.NotificationsColumn.Name, txtSearchNotificationsName.Text);


            NotificationsGrid.DataSource = GPSTrackingDBV2.Data.DataRepository.NotificationsProvider.Find(NotificationsQuery);
            NotificationsGrid.DataBind();
        }

        protected void btnAddNotifications_Click(object sender, EventArgs e)
        {
            if (NotificationsGrid.SelectedItems.Count > 0)
            {
                Telerik.Web.UI.GridDataItem row = NotificationsGrid.SelectedItems[0] as Telerik.Web.UI.GridDataItem;
                OnAddNotificationsEvent(row);
            }
        }

        protected void btnNotificationsOK_Click(object sender, EventArgs e)
        {
            if (NotificationsGrid.SelectedItems.Count > 0)
            {
                txtNotificationsID.Text = NotificationsGrid.SelectedItems[0].Cells[3].Text;
                txtNotificationsName.Text = NotificationsGrid.SelectedItems[0].Cells[4].Text;

                Telerik.Web.UI.GridDataItem row = NotificationsGrid.SelectedItems[0] as Telerik.Web.UI.GridDataItem;

                if (OnAcceptEvent != null) OnAcceptEvent(row);
            }
        }
    }
}